package cn.monkey.family.server.domain.uc.auth.token;

import cn.monkey.family.data.context.RequestContext;
import org.springframework.cache.Cache;
import org.springframework.cache.CacheManager;

import java.util.Objects;

public class CacheableRequestContextRepository implements RequestContextRepository {

    private final Cache cache;

    public CacheableRequestContextRepository(CacheManager cacheManager) {
        this.cache = Objects.requireNonNull(cacheManager.getCache(RequestContextRepository.KEY));
    }

    @Override
    public RequestContext get(String key) {
        RequestContext s = this.cache.get(key, RequestContext.class);
        if (s != null) {
            this.cache.evict(key);
        }
        return s;
    }

    @Override
    public void set(String key, RequestContext value) {
        this.cache.put(key, value);
    }
}
